package com.yunqing.restkeeper.service.common.dubbo;

import com.yunqing.restkeeper.common.tenant.TenantContext;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.common.extension.Activate;
import org.apache.dubbo.rpc.*;

/**
 * @ClassName DubboConsumerContextFilter
 * @Description TODO
 * @author: puyunqing
 * @Date: 2025/7/4
 */
@Activate //开启了dubbo的扩展
@Slf4j
public class DubboConsumerContextFilter implements Filter {

    @Override
    public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcException {

        log.info("shopId-----------" + TenantContext.getShopId());

        log.info("storeId-----------" + TenantContext.getStoreId());

        log.info("ThreadName-------------" + Thread.currentThread().getName());

        //从自定义的上下文对象中获取到令牌的相关信息，然后给他存入到RPCCONTEXT
        RpcContext.getClientAttachment().setAttachment("shopId", TenantContext.getShopId());
        RpcContext.getClientAttachment().setAttachment("loginUserId", TenantContext.getLoginUserId());
        RpcContext.getClientAttachment().setAttachment("loginUserName", TenantContext.getLoginUserName());
        RpcContext.getClientAttachment().setAttachment("storeId", TenantContext.getStoreId());


        return invoker.invoke(invocation);
    }

}
